package com.koron.system.eform;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.koron.bean.base.Response;
import com.koron.bean.system.menu.MenuBean;
import com.koron.common.core.business.system.tree.TreeUtil;
import com.koron.common.core.util.CodeTools;
import com.koron.system.eform.bean.FormStoreBean;
import com.koron.system.eform.bean.FormStoreTreeVo;
import com.koron.system.eform.mapper.FormStoreBeanMapper;
import com.koron.system.menu.mapper.MenuMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class FormStoreServiceImpl {

    public static final String EFORM = "/eform";

    @TaskAnnotation("insertForm")
    public Response insertForm(SessionFactory factory, FormStoreBean bean, Boolean isroot) {
        FormStoreBeanMapper mapper = factory.getMapper(FormStoreBeanMapper.class);
        MenuMapper menumapper = factory.getMapper(MenuMapper.class);
        if (isroot) {
            MenuBean query = new MenuBean();
            query.setId(bean.getParentid());
            query.setMenuUrl(EFORM);
            MenuBean menuBean = menumapper.querySingleMenu(query);
            menumapper.updateMenu(menuBean);
        }
        bean.setId(CodeTools.getCode32());
        mapper.insert(bean);
        return Response.ok(bean.getId());
    }

    @TaskAnnotation("updateForm")
    public Response updateForm(SessionFactory factory, FormStoreBean bean) {
        FormStoreBeanMapper mapper = factory.getMapper(FormStoreBeanMapper.class);
        mapper.updateByPrimaryKey(bean);
        return Response.ok(bean.getId());
    }

    @TaskAnnotation("deleteForm")
    public Response deleteForm(SessionFactory factory, String id) {
        FormStoreBeanMapper mapper = factory.getMapper(FormStoreBeanMapper.class);
        mapper.updateDeleteFlagById(1, id);
        return Response.ok();
    }

    @TaskAnnotation("treeForm")
    public Response treeForm(SessionFactory factory, String id) {
        FormStoreBeanMapper mapper = factory.getMapper(FormStoreBeanMapper.class);
        List<FormStoreTreeVo> listvo = new ArrayList<>();
        List<FormStoreBean> list = mapper.queryAll();
        for (FormStoreBean formStoreBean : list) {
            listvo.add(Convert.convert(FormStoreTreeVo.class, formStoreBean));
        }
        List<FormStoreTreeVo> alltree = TreeUtil.getTree(listvo, 1);
        List<FormStoreTreeVo> tree = alltree.stream().filter(f -> f.getParentid().equals(id)).collect(Collectors.toList());
        return Response.ok(tree);
    }

    @TaskAnnotation("formExchangeSort")
    public Response formExchangeSort(SessionFactory factory, String oneId, String twoId) {
        FormStoreBeanMapper mapper = factory.getMapper(FormStoreBeanMapper.class);
        FormStoreBean one = mapper.selectByPrimaryKey(oneId);
        FormStoreBean two = mapper.selectByPrimaryKey(twoId);
        int onesort = one.getSort();
        int twosort = two.getSort();
        mapper.updatesortById(onesort, two.getId());
        mapper.updatesortById(twosort, one.getId());
        return Response.ok();
    }

    @TaskAnnotation("mobileNode")
    public Response mobileNode(SessionFactory factory, String id, String paretId) {
        FormStoreBeanMapper mapper = factory.getMapper(FormStoreBeanMapper.class);
        FormStoreBean paret = mapper.selectByPrimaryKey(paretId);
        Assert.isFalse(ObjectUtil.isNull(paret), "父节点不存在");
        mapper.updateParentidById(paretId, id);
        return Response.ok();
    }

}
